package com.css.zfzx.fxpg.common.init.service.impl;

import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.zfzx.fxpg.common.init.service.InitialService;
import com.css.zfzx.fxpg.modules.casulaty.estimate.repository.entity.FxCasulatyEstimateEntity;
import com.css.zfzx.fxpg.modules.casulaty.probability.repository.entity.FxCasulatyProbabilityEntity;
import com.css.zfzx.fxpg.modules.economicloss.middle.repository.entity.FxEconomyLossMiddleEntity;
import com.css.zfzx.fxpg.modules.economicloss.probability.repository.entity.FxEconomyLossProbabilityComputeEntity;
import com.css.zfzx.fxpg.modules.middle.repository.entity.FxMiddleResultEntity;
import com.css.zfzx.fxpg.modules.percent.casualty.repository.entity.FxCasualtyPercentEntity;
import com.css.zfzx.fxpg.modules.percent.economicloss.repository.entity.FxEconomicLossEntity;
import com.css.zfzx.fxpg.modules.risk.repository.entity.FxRiskResultEntity;
import com.css.zfzx.fxpg.modules.vulnerability.danger.repository.entity.FxVulnerabilityDangerEntity;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author leon
 * @Date: 2021/2/3 2:23 下午
 */
@Service
public class InitialServiceImpl implements InitialService {
    private static List<HashMap<String, String>> divisionList;

    public static void setDivisionList() {
        if (CollectionUtils.isEmpty(divisionList)) {
            divisionList = initializeDivision();
        }
    }


    /**
     * 初始化省市区区划list
     */
    private static List<HashMap<String, String>> initializeDivision() {
        List<HashMap<String, String>> divisionList = new ArrayList<>();
        List<DivisionEntity> provinceDivisions = PlatformAPI.getDivisionAPI().getSubDivisions("root");
        for (DivisionEntity provinceDivision : provinceDivisions) {
            // 省
            String province = provinceDivision.getDivisionName();
            // 直辖市
            if ("北京市".equals(province) || "天津市".equals(province) || "上海市".equals(province) || "重庆市".equals(province)) {
                // 市
                String city = province;
                List<DivisionEntity> areaDivisions = PlatformAPI.getDivisionAPI().getSubDivisions(provinceDivision.getDivisionId());
                for (DivisionEntity areaDivision : areaDivisions) {
                    // 区
                    String area = areaDivision.getDivisionName();
                    HashMap<String, String> map = new HashMap<>();
                    map.put("province", province);
                    map.put("city", city);
                    map.put("area", area);
                    divisionList.add(map);
                }
            }
            // 省下面的正常市
            else {
                List<DivisionEntity> cityDivisions = PlatformAPI.getDivisionAPI().getSubDivisions(provinceDivision.getDivisionId());
                for (DivisionEntity cityDivision : cityDivisions) {
                    // 市
                    String city = cityDivision.getDivisionName();
                    List<DivisionEntity> areaDivisions = PlatformAPI.getDivisionAPI().getSubDivisions(cityDivision.getDivisionId());
                    for (DivisionEntity areaDivision : areaDivisions) {
                        // 区
                        String area = areaDivision.getDivisionName();
                        HashMap<String, String> map = new HashMap<>();
                        map.put("province", province);
                        map.put("city", city);
                        map.put("area", area);
                        divisionList.add(map);
                    }
                }
            }
        }
        return divisionList;
    }


    @Override
    public List<FxCasulatyEstimateEntity> listInitializeCE() {
        setDivisionList();
        List<FxCasulatyEstimateEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            // 烈度 6 7 8 9 10
            String[] intensities = "6,7,8,9,10".split(",");
            for (String intensName : intensities) {
                String[] casulaties = "重伤,死亡".split(",");
                // 人员伤亡
                for (String casulaty : casulaties) {
                    String[] periods = "白天,夜间".split(",");
                    // 时间段
                    for (String period : periods) {
                        // 破坏程度 1 2 3 4
                        String[] damageDegrees = "1,2,3,4".split(",");
                        for (String damageDegreeName : damageDegrees) {
                            FxCasulatyEstimateEntity entity = new FxCasulatyEstimateEntity();
                            entity.setProvince(map.get("province"));
                            entity.setCity(map.get("city"));
                            entity.setArea(map.get("area"));
                            entity.setIntensity(intensName);
                            entity.setCasulaty(casulaty);
                            entity.setPeriod(period);
                            entity.setDamageDegree(damageDegreeName);
                            // 里面其实只是存了该实体的地址
                            list.add(entity);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<FxEconomicLossEntity> listInitializeEL() {
        setDivisionList();
        List<FxEconomicLossEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            // 结构类型
            String[] structureTypes = "单层砖混,2-3层砖混,多层砖混,钢混框架,钢混框剪".split(",");
            for (String structureType : structureTypes) {
                FxEconomicLossEntity entity = new FxEconomicLossEntity();
                entity.setProvince(map.get("province"));
                entity.setCity(map.get("city"));
                entity.setArea(map.get("area"));
                entity.setStructureType(structureType);
                // 里面其实只是存了该实体的地址
                list.add(entity);
            }
        }
        return list;
    }

    @Override
    public List<FxCasualtyPercentEntity> listInitializeCP() {
        setDivisionList();
        List<FxCasualtyPercentEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            // 破坏程度 0 1 2 3 4
            String[] damageDegrees = "0,1,2,3,4".split(",");
            for (String damageDegree : damageDegrees) {
                FxCasualtyPercentEntity entity = new FxCasualtyPercentEntity();
                entity.setProvince(map.get("province"));
                entity.setCity(map.get("city"));
                entity.setArea(map.get("area"));
                entity.setDamageDegree(damageDegree);
                // 里面其实只是存了该实体的地址
                list.add(entity);
            }
        }
        return list;
    }

    @Override
    public List<FxCasulatyProbabilityEntity> listInitializeCPro() {
        setDivisionList();
        List<FxCasulatyProbabilityEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            String[] casulaties = "重伤,死亡".split(",");
            // 人员伤亡
            for (String casulaty : casulaties) {
                // 时间段
                String[] periods = "白天,夜间".split(",");
                for (String period : periods) {
                    // 破坏程度 1 2 3 4
                    String[] damageDegrees = "1,2,3,4".split(",");
                    for (String damageDegreeName : damageDegrees) {
                        FxCasulatyProbabilityEntity entity = new FxCasulatyProbabilityEntity();
                        entity.setProvince(map.get("province"));
                        entity.setCity(map.get("city"));
                        entity.setArea(map.get("area"));
                        entity.setCasulaty(casulaty);
                        entity.setPeriod(period);
                        entity.setDamageDegree(damageDegreeName);
                        // 里面其实只是存了该实体的地址
                        list.add(entity);
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<FxMiddleResultEntity> listInitializeMR() {
        setDivisionList();
        List<FxMiddleResultEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            // 烈度 6 7 8 9 10
            String[] intensities = "6,7,8,9,10".split(",");
            for (String intensity : intensities) {
                // 破坏程度 0 1 2 3 4
                String[] damageDegrees = "0,1,2,3,4".split(",");
                for (String damageDegreeName : damageDegrees) {
                    FxMiddleResultEntity entity = new FxMiddleResultEntity();
                    entity.setProvince(map.get("province"));
                    entity.setCity(map.get("city"));
                    entity.setArea(map.get("area"));
                    entity.setEarthquakeIntensity(intensity);
                    entity.setDamageDegree(damageDegreeName);
                    // 里面其实只是存了该实体的地址
                    list.add(entity);
                }
            }
        }
        return list;
    }

    @Override
    public List<FxEconomyLossMiddleEntity> listInitializeELM() {
        setDivisionList();
        List<FxEconomyLossMiddleEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            // 烈度 6 7 8 9 10
            String[] intensities = "6,7,8,9,10".split(",");
            for (String intensity : intensities) {
                // 破坏程度 0 1 2 3 4
                String[] damageDegrees = "0,1,2,3,4".split(",");
                for (String damageDegreeName : damageDegrees) {
                    FxEconomyLossMiddleEntity entity = new FxEconomyLossMiddleEntity();
                    entity.setProvince(map.get("province"));
                    entity.setCity(map.get("city"));
                    entity.setArea(map.get("area"));
                    entity.setEarthquakeIntensity(intensity);
                    entity.setDamageDegree(damageDegreeName);
                    // 里面其实只是存了该实体的地址
                    list.add(entity);
                }
            }
        }
        return list;
    }

    @Override
    public List<FxEconomyLossProbabilityComputeEntity> listInitializeELPC() {
        setDivisionList();
        List<FxEconomyLossProbabilityComputeEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            // 破坏程度 0 1 2 3 4
            String[] damageDegrees = "0,1,2,3,4".split(",");
            for (String damageDegreeName : damageDegrees) {
                FxEconomyLossProbabilityComputeEntity entity = new FxEconomyLossProbabilityComputeEntity();
                entity.setProvince(map.get("province"));
                entity.setCity(map.get("city"));
                entity.setArea(map.get("area"));
                entity.setDamageDegree(damageDegreeName);
                // 里面其实只是存了该实体的地址
                list.add(entity);
            }

        }
        return list;
    }

    @Override
    public List<FxVulnerabilityDangerEntity> listInitializeDanger() {
        setDivisionList();
        List<FxVulnerabilityDangerEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            FxVulnerabilityDangerEntity entity = new FxVulnerabilityDangerEntity();
            entity.setProvince(map.get("province"));
            entity.setCity(map.get("city"));
            entity.setArea(map.get("area"));
            // 里面其实只是存了该实体的地址
            list.add(entity);
        }
        return list;
    }

    @Override
    public List<FxRiskResultEntity> listInitializeRisk() {
        setDivisionList();
        List<FxRiskResultEntity> list = new ArrayList<>();
        for (HashMap<String, String> map : divisionList) {
            FxRiskResultEntity entity = new FxRiskResultEntity();
            entity.setProvince(map.get("province"));
            entity.setCity(map.get("city"));
            entity.setArea(map.get("area"));
            // 里面其实只是存了该实体的地址
            list.add(entity);
        }
        return list;
    }

}
